package com.liyuanjinglyj.csdncard.slice;

import com.liyuanjinglyj.csdncard.ResourceTable;
import com.liyuanjinglyj.csdncard.utils.LYJUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormBindingData;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Picker;
import ohos.agp.window.service.WindowManager;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.Revocable;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONArray;
import ohos.utils.zson.ZSONObject;
import java.util.*;

public class CSDNChoiceBlogAbilitySlice extends AbilitySlice {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, CSDNChoiceBlogAbilitySlice.class.getName());
    private Picker picker;
    private Button button;
    private Long formId;
    private Map<String,String> csdnBlog=new HashMap<>();
    private String url="https://blog.csdn.net/qq_39046854";
    private static List<String> headImgUrl=new ArrayList<>();
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_csdnchoice_blog);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);
        formId=intent.getLongParam(AbilitySlice.PARAM_FORM_IDENTITY_KEY, -1);
        init();
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        this.button=(Button)findComponentById(ResourceTable.Id_ability_csdnchoice_blog_button);
        this.picker=(Picker) findComponentById(ResourceTable.Id_ability_csdnchoice_blog_picker);
        String[] keyStr= Arrays.stream(csdnBlog.keySet().toArray()).toArray(String[]::new);
        this.picker.setDisplayedData(keyStr);
        this.picker.setWheelModeEnabled(true);
        this.picker.setMaxValue(csdnBlog.size()-1);
        this.picker.setValueChangedListener(new Picker.ValueChangedListener() {
            @Override
            public void onValueChanged(Picker picker, int i, int i1) {
                if(i1>=0 && i1<csdnBlog.size()){
                    url=csdnBlog.get(keyStr[i1]);
                }

            }
        });
        this.button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                try {
                    HiLog.info(TAG, url);
                    TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);
                    Revocable revocable = globalTaskDispatcher.asyncDispatch(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                ZSONArray zsonArray=new ZSONArray();
                                ZSONObject zsonObject=new ZSONObject();
                                LYJUtils.getData(zsonObject,zsonArray,url);
                                getUITaskDispatcher().asyncDispatch(new Runnable() {
                                    @Override
                                    public void run() {
                                        zsonObject.put("blogList",zsonArray);
                                        FormBindingData bindingData = new FormBindingData(zsonObject);
                                        try {
                                            getAbility().updateForm(formId,bindingData);
                                            terminateAbility();
                                        } catch (FormException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Exception e) {
                    HiLog.info(TAG, "12w");
                    e.printStackTrace();
                }
            }
        });
    }

    public void init(){
        this.csdnBlog.put("老猿Python","https://blog.csdn.net/LaoYuanPython");
        this.csdnBlog.put("李元静","https://blog.csdn.net/liyuanjinglyj");
        this.csdnBlog.put("大家一起学编程","https://blog.csdn.net/qq_39046854");
        this.csdnBlog.put("主打Python","https://blog.csdn.net/weixin_54733110");
        this.csdnBlog.put("CHQIUU","https://blog.csdn.net/QIU176161650");
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
